
// 新增
$(function () {
    $("#save-caseplan-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#addcaseplan-dialog");
        //获取元素
        var plannameE = $("input[name=planname]");
        var planname = plannameE.val();

        var case_run_wayE = $("input[name=case_run_way]");
        var case_run_way = case_run_wayE.val();

        var delay_timeE = $("input[name=delay_time]");
        var delay_time = delay_timeE.val();

        var case_run_way_01 = $('input[name="case_run_way_01"]:checked').val();
        var group = $("#group option:checked").val();

        var submitType = self.attr("data-type"); //编辑的时候动态绑定该属性
        var plan_id = self.attr("data-id"); //编辑的时候动态绑定该属性

        if(!planname){
            amanalert.alertInfo("计划名称不能为空！");
            return;
        }

        var url = '';
        if(submitType == 'update'){
            url = '/updatecaseplan/';
            res_message = '用例计划修改成功！'
        }else {
            url = '/addcaseplan/';
            res_message = '用例计划新增成功！'
        }
        amanajax.post({
            'url':url,
            'data':{
                'plan_name':planname,
                'case_run_way':case_run_way,
                'case_run_way_01':case_run_way_01,
                'delay_time':delay_time,
                'plan_group':group,
                'plan_id':plan_id
            },
            'success':function (data) {
                if(data['code'] == 1){
                    dialog.modal('hide');
                    amanalert.alertSuccessToast(res_message);
                    window.location.reload();
                }else {
                    amanalert.alertInfo(data['message']);
                }
            },
            'fail':function (error) {
                // console.log(error);
                amanalert.alertNetworkError();
            }
        });
    });
});

// 编辑
$(function () {
    $(".edit-caseplan-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#addcaseplan-dialog");

        dialog.modal("show");
        var tr = self.parent().parent();

        var planname = tr.attr("data-plan_name");
        var plannameE = dialog.find("input[name='planname']");
        var case_run_way = tr.attr("data-case_run_way");
        var case_run_wayE = dialog.find("input[name='case_run_way']");
        var case_run_way_01 = tr.attr("data-case_run_way_01");
        var case_run_way_01E = dialog.find("input[name='case_run_way_01']");
        var delay_time = tr.attr("data-delay_time");
        var delay_timeE = dialog.find("input[name='delay_time']");

        var group = tr.attr("data-group");
        var group_selector = document.getElementById("group");
        for(var i=0;i<group_selector.options.length;i++){
            if(group_selector.options[i].text == group){
                group_selector.options[i].selected = true;
                break;
            }
        }

        var saveBtn = dialog.find("#save-caseplan-btn");

        plannameE.val(planname);
        case_run_wayE.val(case_run_way);
        delay_timeE.val(delay_time);
        //单选处理
        if(case_run_way_01 == '分钟'){
            $("#count").attr("checked", false);
            $("#miniute").attr("checked", true);
        }


        saveBtn.attr("data-type", "update"); // 给保存按钮绑定一个data-type属性，值为update
        saveBtn.attr("data-id", tr.attr("data-id"));

    });
});

// 执行/停止
$(function () {
    $(".status-caseplan-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var tr = self.parent().parent();
        var plan_id = tr.attr('data-id');
        var statusText = this.value;
        var case_count = tr.attr("data-case_count");
        var plan_group = tr.attr("data-plan_group");
        var gp_status = tr.attr("data-gp_status");
        var case_run_way = tr.attr("data-case_run_way");
        var case_run_way_01 = tr.attr("data-case_run_way_01");
        var delay_time = tr.attr("data-delay_time");

        amanajax.post({
            url: "/updatestatus_plan/",
            data:{
                'plan_id':plan_id,
                'status':statusText,
                'case_count':case_count,
                'plan_group':plan_group,
                'gp_status':gp_status,
                'case_run_way':case_run_way,
                'case_run_way_01':case_run_way_01,
                'delay_time':delay_time
            },
            'success':function (data) {
                if(data['code'] == 1){
                    window.location.reload();
                }else {
                    amanalert.alertConfirm({
                        "msg":data['message'],
                        "confirmCallback":function () {
                            window.location.reload();
                        },
                        "cancelCallback":function () {
                            window.location.reload();
                        }
                    });
                }
            },
            'fail':function (error) {
                amanalert.alertNetworkError();
            }
        });
    });
});

// 删除
$(function () {
    $(".delete-caseplan-btn").click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var plan_id = tr.attr('data-id');
        amanalert.alertConfirm({
            "msg":"确定删除这个用例计划吗？",
            "confirmCallback":function () {
                amanajax.post({
                    'url':'/delcaseplan/',
                    'data':{
                        'plan_id':plan_id
                    },
                    'success':function (data) {
                        console.log(data);
                        if(data['code'] == 1){
                            amanalert.alertSuccessToast('删除成功！');
                            window.location.reload();
                        }else {
                            amanalert.alertToast(data['message']);
                            window.location.reload();
                        }
                    },
                    'fail':function (error) {
                        amanalert.alertNetworkError();
                    }
                })
            }
        });
    });
});